package com.jy.adscm.spss.utils;

import com.jy.adscm.spss.bean.IncomeStacking;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtils {
    /**
     * 根据传过来的日期返回相应的周{2018:11}
     * @param time
     * @return
     * @throws ParseException
     */
    public static String  getWeek(Date time) throws ParseException {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }

        int week=cal.get(Calendar.WEEK_OF_YEAR);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        if (week<10) {
            if(sdf.format(time).substring(5,7).equals("12")){
                if (week==1){
                    return Integer.valueOf(sdf.format(time).substring(0,4))+1+":0"+week;
                }
            }
            return sdf.format(time).substring(0,4) +":0"+week;
        }else {

            return sdf.format(time).substring(0,4) +":"+week;
        }

    }

    /**
     * 根据年和周获取对应的日期范围
     * @param year
     * @param week
     * @return
     */
    public static String getWeekDays(int year,int week){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.WEEK_OF_YEAR, week);
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.set(Calendar.DAY_OF_WEEK,cal.getFirstDayOfWeek());
        Date date = cal.getTime();
        SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd");
        cal.add(Calendar.DAY_OF_WEEK, 6);
        String endDate = sd.format(cal.getTime());
        return sd.format(date).substring(5,10)+"~"+endDate.substring(5,10);
    }

    /**
     * 返回当前年和第二年的所有周
     * @param
     * @return
     */
    public static List<String> getTwoYearWeek(){
        SimpleDateFormat sd=new SimpleDateFormat("yyyy");
        List<String> list=new ArrayList<String>();
        int k=2018;
        String thisYear = sd.format(new Date());
        for (int j=k;j<=Integer.valueOf(thisYear)+1;j++){
            if (j==2018){
                for(int i =30; i<=52;i++){
                    if (i<10){
                        list.add(j+":0"+i);
                    }else {
                        list.add(j+":"+i);
                    }

                }
            }else {
                for(int i =1; i<=52;i++){
                    if (i<10){
                        list.add(j+":0"+i);
                    }else {
                        list.add(j+":"+i);
                    }

                }
            }

        }
        return list;
    }
    /**
     * 将字符串转换为日期
     * @param date 格式为：yyyy-MM-dd
     * @return
     */
    public static Date parseDate(String date){

        if(date != null && !"".equals(date)){
            if(date.length() > 10){
                date = date.substring(0, 10);
            }
            SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date d = dfs.parse(date);
                return d;
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    /**
     * 格式化指定日期，格式：yyyy-MM-dd
     * @return
     */
    public static String getNowOnlyDateFomat1(Date date){
        String dfs = new SimpleDateFormat("yyyy-MM-dd").format(date);
        return dfs;
    }
    public static String getThatDate(String date,int num,int type){
        Calendar calendar = Calendar.getInstance();
        Date d = parseDate(date);
        calendar.setTime(d);
        calendar.add(type, num);
        d = calendar.getTime();
        return getNowOnlyDateFomat1(d);
    }
    /**
     * 获取某年某月的最后一天
     * @param year
     * @param month
     * @return
     */
    public static String getLastDayOfMonth1(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DATE);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd");
        String time ="2018-12-31";
        System.out.println(getWeekDays(2018,33));
    }

    /**
     * 绘制甘特图工具类 、保证数据和周长度相同
     *
     * @param list
     * @return
     */

    public static List<IncomeStacking> getListLen(List<IncomeStacking> list){
        List<String> onlyWeek = getTwoYearWeek();
        double tmp=0;
        String OBJECT=null;
        String jhyf=null;
        String whereWeek=null;

        List<IncomeStacking> tmpList=new ArrayList<IncomeStacking>();
        for (String string : onlyWeek) {
            for (IncomeStacking sta : list) {
                if (sta.getWHEREWEEK().equals(string)) {

                    OBJECT=sta.getOBJECT();
                    jhyf=sta.getJHYF();
                    whereWeek=sta.getWHEREWEEK();
                    tmp+=Double.valueOf(sta.getJHJE());
                }
            }


            if (tmp!=0) {
                IncomeStacking income=new IncomeStacking();
                income.setJHJE(String.valueOf(tmp));
                income.setJHYF(jhyf);
                income.setOBJECT(OBJECT);
                income.setWHEREWEEK(whereWeek);
                tmpList.add(income);
            }

            tmp=0;

        }
        List<String> tmpWeek =new ArrayList<String>();


        for (IncomeStacking income : tmpList) {
            tmpWeek.add(income.getWHEREWEEK());
        }
        onlyWeek.removeAll(tmpWeek);

        for (String string : onlyWeek) {
            IncomeStacking incomeStacking=new IncomeStacking();
            incomeStacking.setJHJE("0");
            incomeStacking.setJHYF(null);
            incomeStacking.setNAME(null);
            incomeStacking.setOBJECT(null);
            incomeStacking.setWHEREWEEK(string);
            tmpList.add(incomeStacking);
        }
        Collections.sort(tmpList);



        return tmpList;

    }

}
